//app.js
App({
  onLaunch: function () {
    if (wx.setStorageSync('user')) return;
    wx.login({
      success: function (res) {
        var userData = {};
        userData.code = res.code;
        if (res.code) {
          wx.getUserInfo({
            success: function (res) {
              wx.setStorageSync('user', res.userInfo);
             // console.log(res.userInfo.avatarUrl);
              wx.request({
                url: 'https://mp.dunkee.cn/banshouli/index.php/Login/login',
                method: "POST",
                data: { 
                  code: userData.code,
                  nickName: res.userInfo.nickName,
                  avatar: res.userInfo.avatarUrl,
                },
                dataType: 'JSON',
                header: {
                  'content-type': 'application/x-www-form-urlencoded'
                },
                success: function (res) {
                  var data = JSON.parse(res.data);
                  var session = {
                    'key': data.session,
                    'openid': data.openid,
                    'time': parseInt(Date.parse(new Date()) * 1000) + parseInt(data.overtime)
                  };
                  //console.log(session);
                  wx.setStorageSync('session', session);
                }
              });
            }
          });
        } else {
          console.log('获取用户登录态失败！' + res.errMsg);
        }
      }
    });
  },
});